home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: malloc question
- Date: 10 Mar 1996 12:16:24 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hvdaoINN1im@keats.ugrad.cs.ubc.ca>
- References: <4htonk$350@news.hklink.net> <4huctt$arv@sparcserver.lrz-muenchen.de> <314318AF.30F@iperbole.bologna.it>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <314318AF.30F@iperbole.bologna.it>,
- Enrico Persiani <vos0225@iperbole.bologna.it> wrote:
-
- [ reformatted to 79 columns ]
-
- >> Simple answer: You don't have to use that cast, and you should _not_ use
- >> it, because all you can do with that cast is _hide_ an error.
-
- >Error?!? Why do you think that casting is an error? The standard ANSI runtime
- >library says:
-
- [ snip ]
-
- >So, if he needs to use that pointer returned by malloc function he have to
- >convert it from a 'void' pointer to a 'item' pointer ( PITEM ). Many compilers
- >don't need the use of casting. It's only a good programming style. But
-
- _Standard_ compilers don't need the cast. The ANSI standard blesses void * as
- an object which can store a pointer of any type.
-
- >remember: malloc returns a 'void' pointer because it doesn't know wich kind of
- >data you'll store in the allocated mem block!
-
- Correction: it returns a _strictly aligned_ void pointer. Just any old void
- pointer cannot be converted to point to arbitrary data types. I could cast a
- char pointer to void, and then cause a serious error by converting it to a
- pointer to int, for example.
-
- >> OTOH, if you are using a C++ compiler to translate C programs, the cast
- >> is needed, but malloc() is obsolete.
-
- >C++ compilers are more rigorous! But if you write a strict-ANSI C program a c++
- >compiler won't return any error...
-
- Even if your C program uses as variable names those identifiers which represent
- reserved identifiers in C++?
-
- What does your C++ compiler do when I feed it this following ANSI/ISO compliant
- program?
-
- #include <stdio.h>
-
- typedef int new;
-
- int main()
- {
- new x = 3;
- return x;
- }
-
- An ANSI compiler will compile the above. What about GNU g++?
-
- test4.c:3: parse error before `new'
- test4.c: In function `int main()':
- test4.c:7: parse error before `='
- test4.c:8: `x' undeclared (first use this function)
- test4.c:8: (Each undeclared identifier is reported only once
- test4.c:8: for each function it appears in.)
-
- How about the HP-UX CC compiler (which uses cfront to translate C++ to C?)
-
-
- CC: "test4.c", line 3: error: syntax error (1502)
- CC: "test4.c", line 7: error: syntax error (1502)
- CC: sorry, cannot recover from previous errors
-
-
- Do you still believe that C++ compilers accept ANSI C programs?
- --
-
-